def find(i):
    used_x[i] = 1
    for j in range(n):
        if not used[j]:
            diff = x[i] + y[j] - data[i][j]
            if diff == 0:
                used[j] = 1
                if girls[j] == -1 or find(girls[j]):
                    girls[j] = i
                    boys[i] = j
                    return True
            else:
                needed[j] = min(diff, needed[j])
    return False


data = [[3, 4, 5], [2, 4, 0], [0, 0, 7]]
m = len(data)
n = len(data[0])
boys = [-1] * m
girls = [-1] * n
needed = [float('inf')] * n
x = [max(data[i]) for i in range(m)]
y = [0] * n
for i in range(m):
    used = [0] * n
    used_x = [0] * m
    while not find(i):
        d = float('inf')
        for j in range(n):
            if not used[j]:
                d = min(needed[j], d)
        for j in range(m):
            if used_x[j]:
                x[j] -= d
        for j in range(n):
            if used[j]:
                y[j] += d
        used = [0] * n
        used_x = [0] * m
total = 0
for i in range(m):
    total += data[i][boys[i]]
print(total)



